﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics.Contracts;
using ServiceManager.Utils;
namespace QuotationService.RealtimeQuotation
{
    internal class MinuteDataRepository<TMinuteData, TMinuteDataEntity>
        where TMinuteData : Quotation.MinuteDataBase
        where TMinuteDataEntity : struct
    {
        protected IEntityConverter<TMinuteData, TMinuteDataEntity> converter;
        public MinuteDataRepository(IEntityConverter<TMinuteData, TMinuteDataEntity> converter)
        {
            this.converter = converter;
        }

        /// <summary>
        /// 更新单个商品的分线数据
        /// </summary>
        /// <param name="quotSource"></param>
        /// <param name="minuteData"></param>
        /// <returns></returns>
        public  bool UpdateMinuteData(string quotSource, TMinuteData minuteData)
        {
            Contract.Requires(!string.IsNullOrEmpty(quotSource));
            Contract.Requires(minuteData != null);
            TMinuteDataEntity minuteDataEntity = this.converter.Convert(minuteData);
            byte[] bytes = minuteDataEntity.StructToBytes<TMinuteDataEntity>();
            return MinuteDataChannel.Update(quotSource, minuteData.StkId.LongCode, minuteData.OccurTime.ToHMM(), bytes);
        }
        /// <summary>
        /// 获取单个商品当日历史分线
        /// </summary>
        /// <param name="quotSource"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public  List<TMinuteData> GetMinuteData(string quotSource, string code)
        {
            Contract.Requires(!string.IsNullOrEmpty(quotSource));
            Contract.Requires(!string.IsNullOrEmpty(code));
            List<byte[]> minuteBytesList = MinuteDataChannel.GetAll(quotSource, code);
            List<TMinuteData> rtn = new List<TMinuteData>();
            if (minuteBytesList != null)
            {
                foreach (var bytes in minuteBytesList)
                {
                    TMinuteDataEntity minuteDataEntity = StructHelper.BytesToStruct<TMinuteDataEntity>(bytes);
                    rtn.Add(this.converter.Convert(minuteDataEntity));
                }
            }
            return rtn;
        }
        /// <summary>
        /// 获取全部商品的分线数据快照
        /// </summary>
        /// <param name="quotSource"></param>
        /// <returns></returns>
        public List<TMinuteData> GetMinuteDataSnap(string quotSource)
        {
            Contract.Requires(!string.IsNullOrEmpty(quotSource));
            List<byte[]> minuteBytesList = MinuteDataChannel.GetAll(quotSource);
            List<TMinuteData> rtn = new List<TMinuteData>();
            if (minuteBytesList != null)
            {
                foreach (var bytes in minuteBytesList)
                {
                    TMinuteDataEntity minuteDataEntity = StructHelper.BytesToStruct<TMinuteDataEntity>(bytes);
                    rtn.Add(this.converter.Convert(minuteDataEntity));
                }
            }
            return rtn;
        }
        /// <summary>
        /// 清除单个商品的全部分线数据
        /// </summary>
        /// <param name="quotSource"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public static bool ClearMinuteData(string quotSource, string code)
        {
            Contract.Requires(!string.IsNullOrEmpty(quotSource));
            Contract.Requires(!string.IsNullOrEmpty(code));
            return MinuteDataChannel.Clear(quotSource, code);
        }
        /// <summary>
        /// 清除全部商品的最新分线数据
        /// </summary>
        /// <param name="quotSource"></param>
        /// <returns></returns>
        public static bool ClearMinuteDataSnap(string quotSource)
        {
            Contract.Requires(!string.IsNullOrEmpty(quotSource));
            return MinuteDataChannel.ClearAll(quotSource);
        }

    }
}
